import Vue from 'vue'
import VueRouter from 'vue-router'
import {get} from "../utils/local"
Vue.use(VueRouter)

const routes = [
    {
      path:'/',
      redirect: "/main/index"
    },
    {
      name:"det",
      path:'/det',
      component:()=>import("@/views/det.vue"),
    },
    {
      name:"order",
      path:'/order',
      component:()=>import("@/views/order.vue"),
    },
    {
      name:"login",
      path:'/login',
      component:()=>import("@/views/login.vue"),
    },
    {
      name:"addr",
      path:'/addr',
      component:()=>import("@/views/addr.vue"),
    },
    {
      name:"main",
      path:'/main',
      component:()=>import("@/views/main.vue"),
      children:[
        {
          name:"index",
          path:"index",
          component:()=>import("@/views/index.vue"),
          meta: { // 当前路由对象携带信息
              text:"首页"// 当前页面的名称
           }
        },
        {
          name:"cate",
          path:"cate",
          component:()=>import("@/views/cate.vue"),
          meta: { // 当前路由对象携带信息
            text:"分类"// 当前页面的名称
         }
        },
        {
          name:"car",
          path:"car",
          component:()=>import("@/views/car.vue"),
          meta: { // 当前路由对象携带信息
            text:"购物车"// 当前页面的名称
         }
        },
        {
          name:"my",
          path:"my",
          component:()=>import("@/views/my.vue"),
          meta: { // 当前路由对象携带信息
            text:"我"// 当前页面的名称
         }
        }
      ]
    }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path=="/order"){
     let userinfo=get("userinfo")
     if(userinfo){
      next()
     }else{
        next("/login")
     }
  }else{
    next()
  }
})
export default router
